package studentsinfomanagement.view.administrator;

import studentsinfomanagement.bean.Course;
import studentsinfomanagement.bean.SC;
import studentsinfomanagement.bean.Student;
import studentsinfomanagement.dao.CourseDAO;
import studentsinfomanagement.dao.SCDAO;
import studentsinfomanagement.dao.TeacherDAO;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

public class SCAdministratorView extends JFrame {

    private SCAdministratorView scAdministratorView = null;
    private Student student = null;
    private SCDAO scdao = null;//选课信息数据层操作对象
    private CourseDAO courseDAO = null;//课程信息数据层操作对象
    private TeacherDAO teacherDAO = null;//老师信息数据层操作对象
    private DefaultTableModel tableModel = null;
    private JTable table = null;
    private JScrollPane scrollPane = null;
    private String[] columnNames = null;  // 列名
    private String [][]tableVales= null; //数据

    {
        table = new JTable();
    }

    public SCAdministratorView(Student student){
        scAdministratorView = this;
        scdao = SCDAO.getInstance();
        courseDAO = CourseDAO.getInstance();
        teacherDAO = TeacherDAO.getInstance();
        this.student = student;
        this.setTitle("学生选课预览");//标题
        this.setSize(550,500);//大小
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//关闭操作隐藏界面
        this.setResizable(false);//大小不可调节

        BorderLayout bl = new BorderLayout();
        this.setLayout(bl);

        //课程滚动显示区域
        showStudentsSC();//获取该课程的学生信息信息
        scrollPane = new JScrollPane(table);

        //退出查看界面
        JPanel jp_exit = new JPanel();
        JButton btn_exit = new JButton("退出");
        jp_exit.add(btn_exit);

        //将组件添加到界面
        this.add(scrollPane,BorderLayout.CENTER);
        this.add(jp_exit,BorderLayout.SOUTH);

        //界面可见
        this.setVisible(true);

        //监听器
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int sel_row = table.getSelectedRow();
                int sel_column = table.getSelectedColumn();
                if(sel_row >= 0 && sel_column == 3){//过滤掉非法值
                    String cName = (String) table.getValueAt(sel_row,0);//课程名
                    String tName = (String) table.getValueAt(sel_row,1);//教师名
                    String tno = teacherDAO.getTeacherByName(tName).getTno();//教工号
                    String cno = courseDAO.getCourseByName(cName,tno).getCno();//课程号
                    SC sc = new SC();
                    sc.setSno(student.getSno());
                    sc.setCno(cno);
                    System.out.println("tno:"+tno+",cno:"+cno+",sno:"+student.getSno());
                    boolean result = scdao.delete(sc);
                    if(!result){//删除失败
                        JOptionPane.showMessageDialog(scAdministratorView,"删除选课失败！");
                    }else {//删除成功
                        JOptionPane.showMessageDialog(scAdministratorView,"删除选课成功！");
                        showStudentsSC();
                        revalidate();
                    }
                }
            }
        });

        btn_exit.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                scAdministratorView.setVisible(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

    }

    /**
     * 获取学生选课信息
     */
    public void showStudentsSC(){
        List<SC> scList = scdao.getSelectedCourseBySno(student.getSno());
        columnNames = new String[]{"课程名","授课教师","成绩","操作"};
        tableVales = new String[scList.size()][];
        int i = 0;
        for(SC sc:scList){
            String cno = sc.getCno();
            Course course = courseDAO.getCourseByCno(cno);
            String cName = course.getName();
            String tno = course.getTno();
            String tName = teacherDAO.getTeacherByTno(tno).getName();
            Float grade = sc.getGrade();
            String sGrade = grade.toString();
            tableVales[i] = new String[4];
            tableVales[i][0] = cName;
            tableVales[i][1] = tName;
            tableVales[i][2] = sGrade;
            tableVales[i][3] = "删除";
            i++;
        }
        tableModel = new DefaultTableModel(tableVales,columnNames){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setModel(tableModel);
    }

}
